﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;

namespace HQWeb.SysManage.MenuManage
{
    public partial class MenuEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                VerdictIsLogin();
                if (!Page.IsPostBack)
                {
                    this.txtSign.Text = Request["sign"];
                    if (this.txtSign.Text == "0")  //添加
                    {
                        this.txtParentIdHid.Text = Request["pid"];
                        this.labTitle.Text = "添加菜单信息";

                    }
                    bindList();
                    if (this.txtSign.Text.Trim() == "1")  //编辑
                    {
                        this.txtIdHid.Text = Request["id"];
                        this.labTitle.Text = "编辑菜单信息";

                        fillForm();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }

        #region 绑定父级菜单下拉框
        private void bindList()
        {

            BLL.SysManage.T_PUB_MENU MenuBll = new BLL.SysManage.T_PUB_MENU();
            IList il = MenuBll.GetModelList("");
            parent_id.ClearSelection();
            parent_id.DataSource = il;
            parent_id.DataTextField = "MENU_NAME"; //dropdownlist的Text的字段 
            parent_id.DataValueField = "MENU_ID";//dropdownlist的Value的字段 
            parent_id.DataBind();
            ListItem item = new ListItem("无父级", "0");
            parent_id.Items.Add(item);
            if (this.txtSign.Text.Trim() == "0")
            {
                parent_id.SelectedValue = this.txtParentIdHid.Text;

            }
        }
        #endregion

        #region 填充页面数据
        protected void fillForm()
        {
            BLL.SysManage.T_PUB_MENU MenuBll = new BLL.SysManage.T_PUB_MENU();
            Model.SysManage.T_PUB_MENU MenuModel = new Model.SysManage.T_PUB_MENU();
            int temp;
            int.TryParse(this.txtIdHid.Text, out temp);
            MenuModel = MenuBll.GetModel(temp);

            this.txt_name.Text = MenuModel.MENU_NAME;
            txt_url.Text = MenuModel.URL;
            this.txt_squence.Text = MenuModel.SEQUENCE.ToString();
            this.parent_id.SelectedValue = MenuModel.PARENT_ID.ToString();
            this.txtParentIdHid.Text = MenuModel.PARENT_ID.ToString();
        }
        #endregion

        #region 提交按钮事件
        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.SysManage.T_PUB_MENU MenuBll = new BLL.SysManage.T_PUB_MENU();
                Model.SysManage.T_PUB_MENU MenuModel = new Model.SysManage.T_PUB_MENU();
                MenuModel.MENU_ID = Convert.ToInt32(string.IsNullOrEmpty(this.txtIdHid.Text.Trim().ToString()) ? "-1" : this.txtIdHid.Text.Trim().ToString());
                MenuModel.PARENT_ID = Convert.ToInt32(this.parent_id.SelectedValue.Trim());
                MenuModel.MENU_NAME = this.txt_name.Text.Trim();
                MenuModel.URL = txt_url.Text.Trim();
                MenuModel.SEQUENCE =Convert.ToInt32(txt_squence.Text.Trim());
                if (this.txtSign.Text.Trim() == "0")
                {

                    MenuBll.Add(MenuModel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]),"添加" + MenuModel.MENU_NAME + "菜单",DateTime.Now);
                }
                else if (this.txtSign.Text.Trim() == "1")
                {

                    MenuBll.Update(MenuModel);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "更新" + MenuModel.MENU_NAME + "菜单信息", DateTime.Now);
                }

                StringBuilder strl = new StringBuilder("<script language='javascript'>\n");
                strl.Append("window.parent.location.reload();\n");
                strl.Append("location.href =\"MenuList.aspx?pid=" + this.parent_id.SelectedValue + "\";\n");
                strl.Append("</script>\n");
                HttpContext.Current.Response.Write(strl.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}